package cn.cvs.service;

import cn.cvs.dao.SysUserMapper;
import cn.cvs.pojo.SysUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.Date;
import java.util.List;

@Service
public class SysUserServiceImpl implements SysUserService {
    @Resource
    private SysUserMapper sysUserMapper;

    public SysUserServiceImpl() {
    }

    public boolean add(SysUser sysUser) throws Exception {
        boolean flag = false;
        sysUser.setCreatedTime(new Date());
        if (this.sysUserMapper.add(sysUser) > 0) {
            flag = true;
        }
        return flag;
    }

    public SysUser login(String account, String userPassword) throws Exception {
        SysUser sysUser = this.sysUserMapper.selectByAccount(account);
        if (null != sysUser && !sysUser.getPassword().equals(userPassword)) {
            sysUser = null;
        }

        return sysUser;
    }

    public List<SysUser> getUserList(String realName, Integer roleId, Integer pageIndex, Integer pageSize) throws Exception {
        int pageBegin = (pageIndex - 1) * pageSize;
        return this.sysUserMapper.selectList(realName, roleId, pageBegin, pageSize);
    }

    public SysUser getUserById(Integer id) throws Exception {
        return this.sysUserMapper.selectById(id);
    }

    public boolean update(SysUser sysUser) throws Exception {
        boolean flag = false;
        sysUser.setUpdatedTime(new Date());
        if (this.sysUserMapper.update(sysUser) > 0) {
            flag = true;
        }

        return flag;
    }

    public boolean updatePwd(Integer id, String pwd) throws Exception {
        boolean flag = false;
        if (this.sysUserMapper.updatePwd(id, pwd) > 0) {
            flag = true;
        }
        return flag;
    }

    public SysUser getByAccount(String account) throws Exception {
        return this.sysUserMapper.selectByAccount(account);
    }

    public boolean deleteById(Integer id) throws Exception {
        boolean flag = true;
        SysUser sysUser = this.sysUserMapper.selectById(id);
        File file;
        if (sysUser.getIdPicPath() != null && !sysUser.getIdPicPath().equals("")) {
            file = new File(sysUser.getIdPicPath());
            if (file.exists()) {
                flag = file.delete();
            }
        }

        if (flag && sysUser.getWorkPicPath() != null && !sysUser.getWorkPicPath().equals("")) {
            file = new File(sysUser.getWorkPicPath());
            if (file.exists()) {
                flag = file.delete();
            }
        }

        if (flag && this.sysUserMapper.deleteById(id) < 1) {
            flag = false;
        }

        return flag;
    }

    @Override
    public SysUser getIsPwd(Integer id, String password) {
        SysUser sysUser = this.sysUserMapper.getIsPwd(id,password);
        return sysUser;
    }

    public int getUserCount(String realName, Integer roleId) throws Exception {
        return this.sysUserMapper.selectCount(realName, roleId);
    }
}
